using Prism.Mvvm;
using Prism.Commands;
using System.Collections.ObjectModel;
using CPlatePrintSys.Application.Services;
using CPlatePrintSys.Domain.Models;
using System.Threading.Tasks;

namespace CPlatePrintSys.Win.ViewModels
{
    public class MotherShipManageViewModel : BindableBase
    {
        private readonly IShipConfigService _service;
        public ObservableCollection<MotherShip> MotherShips { get; } = new ObservableCollection<MotherShip>();
        public string NewMotherShipNo { get; set; }
        public DelegateCommand AddMotherShipCommand { get; }
        public DelegateCommand<MotherShip> DeleteMotherShipCommand { get; }

        public MotherShipManageViewModel(IShipConfigService service)
        {
            _service = service;
            AddMotherShipCommand = new DelegateCommand(async () => await AddMotherShip());
            DeleteMotherShipCommand = new DelegateCommand<MotherShip>(async m => await DeleteMotherShip(m));
            LoadData();
        }

        private async void LoadData()
        {
            MotherShips.Clear();
            var list = await _service.GetAllMotherShipsAsync();
            foreach (var m in list)
                MotherShips.Add(m);
        }

        private async Task AddMotherShip()
        {
            if (string.IsNullOrWhiteSpace(NewMotherShipNo)) return;
            await _service.AddMotherShipAsync(NewMotherShipNo.Trim().ToUpper());
            LoadData();
            NewMotherShipNo = string.Empty;
            RaisePropertyChanged(nameof(NewMotherShipNo));
        }

        private async Task DeleteMotherShip(MotherShip m)
        {
            if (m == null) return;
            await _service.DeleteMotherShipAsync(m.Id);
            LoadData();
        }
    }
} 